package com.youzikj.wx.comm.util;

import java.security.MessageDigest;
import java.util.Arrays;

/**
 *请求检验工具类 
 */
public class SignUtil {
	//http://114.215.207.119/start-wx/weixin/interface/validate
	//与接口配置信息中token保持一致
	private static String token = "weiyouyouzikj";
	
	/**
	 * 校验签名
	 * @param signature 微信加密签名x
	 * @param timestamp 时间戳
	 * @param nonce 随机数
	 * @return
	 */
	public static boolean checkSignature(String signature, String timestamp, String nonce){
		String [] paramArr = new String[]{token, timestamp, nonce};
		Arrays.sort(paramArr);
		String content = paramArr[0].concat(paramArr[1]).concat(paramArr[2]);
		String ciphertext = null;
		try {
			MessageDigest md = MessageDigest.getInstance("SHA-1");
			//对拼接后的字符串进行sha1加密
			byte[] digest = md.digest(content.toString().getBytes());
			ciphertext = byteToStr(digest);
		} catch (Exception e) {
			e.printStackTrace();
		}
		//将sha1加密后的字符串与signature进行比较
		return ciphertext != null ? ciphertext.equals(signature.toUpperCase()) :false;
	}
	
	/**
	 * 将字节数组转换为十六进制字符串
	 */
	private static String byteToStr(byte[] byteArray){
		String strDigest = "";
		for(int i = 0; i < byteArray.length; i++){
			strDigest += byteToHexStr(byteArray[i]);
		}
		return strDigest;
	}
	
	/**
	 * 将字节转换成十六进制字符串
	 */
	private static String byteToHexStr(byte mByte){
		char[] Digit = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
		char[] tempArr = new char[2];
		tempArr[0] = Digit[(mByte >>> 4) & 0X0F];
		tempArr[1] = Digit[mByte & 0X0F];
		String s = new String(tempArr);
		return s;
		
	}
	
}
